<?php
/**
 * Blog home
 *
 * @copyright        2017 opencart.cn - All Rights Reserved
 * @link             http://www.guangdawangluo.com
 * @author           Sam Chen <sam.chen@opencart.cn>
 * @created          2017-05-26 10:10:19
 * @modified         2017-05-26 17:17:20
 */

class ControllerBlogHome extends Controller {
	public function index() {
		if (!$this->config->get('blog_status')) {
			return $this->notFound();
		}

		$this->load->language('blog/blog');

		if (isset($this->request->get['page'])) {
			$page = (int)$this->request->get['page'];
		} else {
			$page = 1;
		}

		$data['text_author'] = $this->language->get('text_author');
		$data['text_date_added'] = $this->language->get('text_date_added');

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/home')
		);

		if ($this->config->has('blog_category_image_width')) {
			$blog_category_image_width = $this->config->get('blog_category_image_width');
		} else {
			$blog_category_image_width = 100;
		}

		if ($this->config->has('blog_category_image_height')) {
			$blog_category_image_height = $this->config->get('blog_category_image_height');
		} else {
			$blog_category_image_height = 100;
		}

		if ($this->config->has('blog_post_image_position')) {
			$data['blog_post_image_position'] = $this->config->get('blog_post_image_position');
		} else {
			$data['blog_post_image_position'] = 'left';
		}

		$read_more = $this->config->get('blog_post_read_more');
		if (isset($read_more[$this->config->get('config_language_id')])) {
			$data['button_read_more'] = $read_more[$this->config->get('config_language_id')];
		} else {
			$data['button_read_more'] = $this->language->get('button_read_more');
		}

		$this->load->model('blog/blog');
		$this->load->model('tool/image');

		$meta_title = $this->config->get('blog_meta_title');
		if (!empty($meta_title[$this->config->get('config_language_id')])) {
			$this->document->setTitle($meta_title[$this->config->get('config_language_id')]);
		} else {
			$this->document->setTitle($this->language->get('text_blog'));
		}

		$blog_menu_name = $this->config->get('blog_menu_name');
		if (!empty($blog_menu_name[$this->config->get('config_language_id')])) {
			$data['heading_title'] = $blog_menu_name[$this->config->get('config_language_id')];

			$data['breadcrumbs'][] = array(
				'text' => $blog_menu_name[$this->config->get('config_language_id')],
				'href' => $this->url->link('blog/home'),
			);
		} else {
			$data['heading_title'] = $this->language->get('text_blog');

			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('text_blog'),
				'href' => $this->url->link('blog/home')
			);
		}

		$meta_description = $this->config->get('blog_meta_description');
		$meta_keyword = $this->config->get('blog_meta_keyword');
		$this->document->setDescription(!empty($meta_description[$this->config->get('config_language_id')]) ? $meta_description[$this->config->get('config_language_id')] : '');
		$this->document->setKeywords(!empty($meta_keyword[$this->config->get('config_language_id')]) ? $meta_keyword[$this->config->get('config_language_id')] : '');

		$data['href'] = $this->url->link('blog/home');

		// Posts
		if ($this->config->has('blog_post_limit')) {
			$limit = $this->config->get('blog_post_limit');
		} else {
			$limit = 15;
		}

		$filter_data = array(
			'start'        => ($page - 1) * $limit,
			'limit'        => $limit
		);

		$post_total = $this->model_blog_blog->getTotalPosts($filter_data);
		$posts = $this->model_blog_blog->getPosts($filter_data);

		$data['posts'] = array();
		if ($posts) {
			if ($this->config->has('blog_post_image_status')) {
				$blog_post_image_status = (int)$this->config->get('blog_post_image_status');
			} else {
				$blog_post_image_status = 1;
			}

			$authors = $this->config->get('blog_default_author');
			if (isset($authors[$this->config->get('config_language_id')])) {
				$default_author = $authors[$this->config->get('config_language_id')];
			} else {
				$default_author = $this->language->get('config_name');
			}

			if ($this->config->has('blog_post_image_width')) {
				$blog_post_image_width = $this->config->get('blog_post_image_width');
			} else {
				$blog_post_image_width = 100;
			}

			if ($this->config->has('blog_post_image_height')) {
				$blog_post_image_height = $this->config->get('blog_post_image_height');
			} else {
				$blog_post_image_height = 100;
			}

			if ($this->config->has('blog_post_description_length')) {
				$blog_post_description_length = (int)$this->config->get('blog_post_description_length');
			} else {
				$blog_post_description_length = 200;
			}

			foreach ($posts as $post) {
				$categories = array();
				$results = $this->model_blog_blog->getPostCategories($post['post_id']);
				if ($results) {
					foreach ($results as $result) {
						$categories[] = array(
							'name' => $result['name'],
							'href' => $this->url->link('blog/category',  'blog_category_id=' . $result['category_id'])
						);
					}
				}

				if ($blog_post_image_status) {
					if ($post['image'] && is_file(DIR_IMAGE . $post['image'])) {
						$thumb = $this->model_tool_image->resize($post['image'], $blog_post_image_width, $blog_post_image_height);
					} else {
						$thumb = 0;
					}
				} else {
					$thumb = 0;
				}

				$data['posts'][] = array(
					'thumb'       => $thumb,
					'name'        => $post['name'],
					'author'      => $post['author'] ? $post['author'] : $default_author,
					'date_added'  => date($this->language->get('date_format_short'), strtotime($post['date_added'])),
					'categories'  => $categories,
					'description' => !empty($post['description']) ? html_entity_decode($post['description'], ENT_QUOTES, 'UTF-8') : (utf8_substr(strip_tags(html_entity_decode($post['content'], ENT_QUOTES, 'UTF-8')), 0, $blog_post_description_length) . '..'),
					'href'        => $this->url->link('blog/post', "blog_post_id={$post['post_id']}")
				);
			}

			$pagination = new Pagination();
			$pagination->total = $post_total;
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->url = $this->url->link('blog/home', 'page={page}');

			$data['pagination'] = $pagination->render();
			$data['results'] = sprintf($this->language->get('text_pagination'), ($post_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($post_total - $limit)) ? $post_total : ((($page - 1) * $limit) + $limit), $post_total, ceil($post_total / $limit));
		} else {
			$data['text_empty'] = $this->language->get('text_empty');
			$data['button_continue'] = $this->language->get('button_continue');
			$data['continue'] = $this->url->link('common/home');
		}

		$data['column_left'] = $this->load->controller('common/column_left');
		$data['column_right'] = $this->load->controller('common/column_right');
		$data['content_top'] = $this->load->controller('common/content_top');
		$data['content_bottom'] = $this->load->controller('common/content_bottom');
		$data['footer'] = $this->load->controller('common/footer');
		$data['header'] = $this->load->controller('common/header');

		$this->response->setOutput($this->load->view('blog/category.tpl', $data));
	}

	private function notFound() {
		$this->load->language('blog/blog');

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/home')
		);

		$this->document->setTitle($this->language->get('text_error'));
		$data['heading_title'] = $this->language->get('text_error');
		$data['text_error'] = $this->language->get('text_error');
		$data['button_continue'] = $this->language->get('button_continue');
		$data['continue'] = $this->url->link('common/home');

		$this->response->addHeader($this->request->server['SERVER_PROTOCOL'] . ' 404 Not Found');

		$data['column_left'] = $this->load->controller('common/column_left');
		$data['column_right'] = $this->load->controller('common/column_right');
		$data['content_top'] = $this->load->controller('common/content_top');
		$data['content_bottom'] = $this->load->controller('common/content_bottom');
		$data['footer'] = $this->load->controller('common/footer');
		$data['header'] = $this->load->controller('common/header');

		$this->response->setOutput($this->load->view('error/not_found', $data));
	}
}
